package com.cole.gx095.controller;

import com.cole.gx095.Repository.SaleBillgoodsRepository;
import com.cole.gx095.Repository.SalebillRepository;
import com.cole.gx095.Repository.SalecontractRepository;
import com.cole.gx095.domain.CostVo;
import com.cole.gx095.domain.mapper.CostVOMapper;
import com.cole.gx095.service.SaleBillService;
import com.cole.gx095.controller.utils.CommonRep;
import com.cole.gx095.entity.SaleBillgoods;
import com.cole.gx095.entity.Salebill;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RequestMapping("/saleBillQuery")
@RestController
@CrossOrigin
@Transactional
public class saleBillQueryController {
    @Autowired
    SalebillRepository salebillRepository;
    @Autowired
    SalecontractRepository salecontractRepository;
    @Autowired
    SaleBillgoodsRepository saleBillgoodsRepository;
    @Autowired
    SaleBillService saleBillService;
    @GetMapping("/List")
    CommonRep List(Integer currentPage, Integer pageSize){
        Page<Salebill> page1 = salebillRepository.findAll(PageRequest.of(currentPage - 1, pageSize));
        List<CostVo> costVoList = new ArrayList<>();
        for (Salebill salebill : page1.getContent()) {
            CostVo conver = CostVOMapper.INSTANCT.conver(salebill);
            conver.setBuyUnit(salecontractRepository.getById(salebill.getContractId()).getBuyUnit());

            List<SaleBillgoods> byBillId = saleBillgoodsRepository.findByBillId(salebill.getId());

            conver.setSaleBillgoodsList(byBillId);
            costVoList.add(conver);
        }
        Double sum = salebillRepository.sumAmount();
        costVoList.forEach(System.out::println);
        return CommonRep.ok()
                .data("page",page1)
                .data("List",costVoList)
                .data("sum",sum);
    }

    @PostMapping("/queryBy")
    CommonRep getById( Integer currentPage,
                       Integer pageSize,
                       CostVo costVo){
        Page<Salebill> page1 = saleBillService.findAllBy(costVo, currentPage, pageSize);
        List<CostVo> costVoList = new ArrayList<>();
        Double sum = 0.0;
        for (Salebill salebill : page1.getContent()) {
            sum += salebill.getBillAccount();
            CostVo conver = CostVOMapper.INSTANCT.conver(salebill);
            conver.setBuyUnit(salecontractRepository.getById(salebill.getContractId()).getBuyUnit());

            List<SaleBillgoods> byBillId = saleBillgoodsRepository.findByBillId(salebill.getId());
            conver.setSaleBillgoodsList(byBillId);
            costVoList.add(conver);

            if(costVo.getBuyUnit()!=null)
                if(!conver.getBuyUnit().equalsIgnoreCase(costVo.getBuyUnit())) {
                    costVoList.remove(conver);
                    sum -= salebill.getBillAccount();

                }
        }
        costVoList.forEach(System.out::println);
        return CommonRep.ok()
                .data("page",page1)
                .data("List",costVoList)
                .data("sum",sum);
    }
}
